﻿using RevitToThumbnail.StructuredStorage;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Windows;
using System.Windows.Forms;

namespace RevitToThumbnail
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            WindowStartupLocation = WindowStartupLocation.CenterScreen;
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            
            FolderBrowserDialog openFileDialog = new FolderBrowserDialog(); //选择文件夹
            if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                address.Text = openFileDialog.SelectedPath;
            }

        }

        private void Button1_Click(object sender, RoutedEventArgs e)
        {
            string addresss = address.Text;
           
            DirectoryInfo root = new DirectoryInfo(addresss);
            FileInfo[] files = root.GetFiles("*", SearchOption.AllDirectories);
            try
            {
                foreach (FileInfo file in files)
                {
                    if (file.FullName.EndsWith(".rfa") || file.FullName.EndsWith(".rvt"))
                    {
                        string a = file.FullName.Substring(0, file.FullName.Length - 4);
                        Storage storage = new Storage(file.FullName);

                        if (storage.IsInitialized == false)
                        {
                            Debug.WriteLine("error");
                            return;
                        }
                        Image image = storage.ThumbnailImage.GetPreviewAsImage();
                        Bitmap bitmap = new Bitmap(image);
                        bitmap.Save(a + ".png", ImageFormat.Png);

                    }
                }
                this.Close();
            }
            catch (Exception r) {
                System.Windows.MessageBox.Show(r.ToString());
            }

        }
    }
}
